<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller
{
    public function index(){
        if(isMobile()){
            $where['is_mobile'] = 1;
        }
        return $this->fetch();
    }
    /*
     * 获取书籍源，数据库有则直接返回
     */
    public function getBookList(){
        $name = $_POST['name'];
        if(empty($name)){
            ret_json(array('code'=>40000,'msg'=>'书名为空'));
        }
        $tbook = db('books_tbook')->where('name','like',$name.'%')->select();
        if($tbook){
            foreach ($tbook as &$v){
                $v['gourl'] = url('getBookChapter',array('bid'=>$v['id']));
            }
            ret_json(array('code'=>200,'data'=>$tbook));
        }else{
            $arr = array(
                '1'=>'img',
                '2'=>'url',
                '3'=>'name',
                '4'=>'intro',
                '5'=>'writer',
            );
            $bsite = db('books_site')->where('status','eq','1')->select();
            foreach ($bsite as $b){
                $list = getCurlList($b['serach_url'].$name);
                //$b['tbook_regular'] = '#<div class="result-item result-game-item">.*<img src="(.*)".*<a cpos="title" href="(.*)".*title="(.*)".*<p class="result-game-item-desc">(.*)</p>.*<span class="result-game-item-info-tag-title preBold">.*<span>(.*)</span>#isU';
                $reg = $b['tbook_regular'];
                header("Content-type: text/html; charset=utf-8");
                preg_match($reg, $list, $matchs);
                $list = getPregMatch($reg,$list,0);
                foreach ($list[1] as &$v){
                    $img = db('books_img')->where(array('site'=>$b['id'],'url'=>$v))->find();
                    if($img){
                        $v = $img['img'];
                    }else{
                        $ext = get_extension($v);
                        $img = './public/images/'.time().$b['id'].rand(1,1000).'.'.$ext;
                        http_down($v,$img);
                        $imgInfo['site'] = $b['id'];
                        $imgInfo['url'] = $v;
                        $imgInfo['img'] = $img;
                        db('books_img')->insert($imgInfo);
                        $v = $img;
                    }
                }
                $arrays = array();
                for($i=0;$i<count($list[1]);$i++){
                    $array = array();
                    $array['img'] = $list[1][$i];
                    $array['url'] = $list[2][$i];
                    $array['name'] = $list[3][$i];
                    $array['intro'] = $list[4][$i];
                    $array['writer'] = trim($list[5][$i]);
                    $book = db('books_tbook')->where(array('site_id'=>$b['id'],'writer'=>$array['writer'],'name'=>$array['name']))->find();
                    if(!$book){
                        $array['site_id'] = $b['id'];
                        $array['time'] = time();
                        $bid = db('books_tbook')->insert($array);
                        $array['id'] = $bid;
                    }else{
                        $array['id'] = $book['id'];
                    }
                    $array['gourl'] = url('getBookChapter',array('bid'=>$array['id']));
                    $arrays[$i] = $array;
                }
                ret_json(array('code'=>200,'data'=>$arrays));
            }
        }
    }
    /*
     * 获取书籍章节
     */
    public function getBookChapter($bid){
        if(!$bid){
            $this->error('书籍错误，请联系管理员');
        }
        $tbook = db('books_tbook')->where(array('id'=>$bid))->find();
        $chapter = db('books_chapter')->where(array('tbook_id'=>$bid))->select();
        $this->assign("name",$tbook['name']);
        if(!$chapter){
            $bsite = db('books_site')->where(array('id'=>$tbook['site_id']))->find();
            $bsite['chapter_regular'] = '#<dd> <a style="" href="(.*)">(.*)</a></dd>#isU';
            $list = getCurlList($tbook['url']);
            //$b['tbook_regular'] = '#<div class="result-item result-game-item">.*<img src="(.*)".*<a cpos="title" href="(.*)".*title="(.*)".*<p class="result-game-item-desc">(.*)</p>.*<span class="result-game-item-info-tag-title preBold">.*<span>(.*)</span>#isU';
            $reg = $bsite['chapter_regular'];
            header("Content-type: text/html; charset=utf-8");
            $list = getPregMatch($reg,$list,0);
            $datas = array();
            for($i=0;$i<count($list[1]);$i++){
                $array = array();
                $array['url'] = $bsite['url'].$list[1][$i];
                $array['title'] = $list[2][$i];
                $array['tbook_id'] = $bid;
                $datas[] = $array;
            }
            db('books_chapter')->insertAll($datas);
            $chapter = db('books_chapter')->where(array('tbook_id'=>$bid))->select();
        }
        foreach ($chapter as &$c){
            $c['gourl'] = url('getBookDetail',array('did'=>$c['id']));
        }
        $this->assign("chapter",$chapter);
        return $this->fetch('chapter');
    }
    /*
     * 获取章节详情
     */
    public function getBookDetail($did){
        if(!$did){
            $this->error('章节错误，请联系管理员');
        }
        $chapter = db('books_chapter')->where(array('id'=>$did))->find();
        $tbook = db('books_tbook')->where(array('id'=>$chapter['tbook_id']))->find();
        $this->assign("name",$chapter['title']);
        $this->assign("id",$chapter['id']);
        if(!$chapter['book_detail']){
            $bsite = db('books_site')->where(array('id'=>$tbook['site_id']))->find();
            $list = getCurlList($chapter['url']);
            $reg = $bsite['detail_regular'];
            header("Content-type: text/html; charset=utf-8");
            $list = getPregMatch($reg,$list,1);
            $datas['book_detail'] = $list[1];
            db('books_chapter')->where(array('id'=>$chapter['id']))->update($datas);
            $chapter = db('books_chapter')->where(array('id'=>$did))->find();
        }
        $this->assign("detail",$chapter['book_detail']);
        return $this->fetch('detail');
    }
    /*
     * 获取下一张
     */
    public function getBooks(){
        $id = $_POST['id'];$type=$_POST['type'];
        if(empty($id)||empty($type)){
            ret_json(array('code'=>40000,'msg'=>'参数错误'));
        }else{
            $chapter = db('books_chapter')->where('id','=',$id)->find();
            if($type == 1){
                $detail = db('books_chapter')->where('id','<',$id)->where('tbook_id','=',$chapter['tbook_id'])->order('id desc')->find();
            }else{
                $detail = db('books_chapter')->where('id','>',$id)->where('tbook_id','=',$chapter['tbook_id'])->find();
            }
            if(empty($detail)){
                $msg = $type==1?'没有上一章了':'没有下一章了';
                ret_json(array('code'=>40000,'msg'=>$msg));
            }
            if(empty($detail['content'])){
                $tbook = db('books_tbook')->where(array('id'=>$detail['tbook_id']))->find();
                $bsite = db('books_site')->where(array('id'=>$tbook['site_id']))->find();
                $list = getCurlList($detail['url']);
                $reg = $bsite['detail_regular'];
                header("Content-type: text/html; charset=utf-8");
                $list = getPregMatch($reg,$list,1);
                $datas['book_detail'] = $list[1];
                db('books_chapter')->where(array('id'=>$detail['id']))->update($datas);
                $detail = db('books_chapter')->where(array('id'=>$detail['id']))->find();
            }
            ret_json(array('code'=>200,'data'=>$detail));
        }
    }
}
